package com.yxkj.syh.app.huarong.activities.account.login.pwd;

import android.app.Application;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.launcher.ARouter;
import com.syh.app.basic.base.view_model.BaseViewModel;
import com.syh.app.basic.bindingAdp.view_click.ClickEvent;
import com.syh.app.basic.utils.Tooast;
import com.yxkj.syh.app.huarong.util.UserManager;
import com.syh.app.basic.utils.net.ApiObserver;
import com.trello.rxlifecycle2.android.FragmentEvent;
import com.yxkj.syh.app.huarong.bean.LoginResponse;
import com.yxkj.syh.app.huarong.bean.UserInfoResponse;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.data_center.model.AccountModel;
import com.yxkj.syh.app.huarong.data_center.model.AuthModel;

import java.util.HashMap;
import java.util.Map;

/**
 * 创建者：SYH
 * 时间：2020/5/19 11:16
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class PwdLoginVM extends BaseViewModel<FragmentEvent> {

    public PwdLoginVM(@NonNull Application application) {
        super(application);
    }

    public ObservableField<String> ofPhone = new ObservableField<String>() {
        @Override
        public void set(String value) {
            super.set(value);
            if (!TextUtils.isEmpty(value) && !TextUtils.isEmpty(ofPwd.get())) {
                if (!ofLoginEnable.get()) {
                    ofLoginEnable.set(true);
                }
            } else {
                if (ofLoginEnable.get()) {
                    ofLoginEnable.set(false);
                }
            }
        }
    };
    public ObservableField<String> ofPwd = new ObservableField<String>() {
        @Override
        public void set(String value) {
            super.set(value);
            if (!TextUtils.isEmpty(value) && !TextUtils.isEmpty(ofPhone.get())) {
                if (!ofLoginEnable.get()) {
                    ofLoginEnable.set(true);
                }
            } else {
                if (ofLoginEnable.get()) {
                    ofLoginEnable.set(false);
                }
            }
        }
    };
    public ObservableField<Boolean> ofEyesOpen = new ObservableField<>(Boolean.FALSE);
    public ObservableField<Boolean> ofLoginEnable = new ObservableField<>(Boolean.FALSE);
    public ClickEvent loginClick = new ClickEvent() {
        @Override
        public void onClick(View view) {
            Map<String, Object> pMap = new HashMap<>();
            pMap.put("username", ofPhone.get());
            pMap.put("password", ofPwd.get());
            pMap.put("code", "logistics");

            showLoading("login");
            AuthModel.getAuthModel().login(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<LoginResponse>() {
                @Override
                public void onSuccess(LoginResponse loginResponse) {
                    hideLoading("login");
                    UserManager.getUserManager().saveToken(loginResponse.getData());
                    userInfo();
                }

                @Override
                public void onFailed(int code, String msg) {
                    hideLoading("login");
                    Tooast.warning(msg);
                }
            });
        }
    };

    private void userInfo(){
        AccountModel.getAccountModel().userInfo(mLifecycleProvider.bindToLifecycle(), new ApiObserver<UserInfoResponse>() {
            @Override
            public void onSuccess(UserInfoResponse userInfoResponse) {
                UserManager.getUserManager().saveUser(userInfoResponse.getData());
                ARouter.getInstance().build(ArouterPath.MAIN_ACTIVITY).navigation();
                finish();
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.warning(msg);
            }
        });
    }

}
